package com.meatball.game.util

import com.alibaba.fastjson2.JSON
import com.baidu.aip.speech.AipSpeech
import com.bytedance.speech.speechengine.SpeechEngineDefines
import com.bytedance.speech.speechengine.SpeechEngineGenerator
import com.meatball.game.data.BaiduResponse
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

@Component
class AsrComponent {
    companion object {
        private val logger = LoggerFactory.getLogger(AsrComponent::class.java)
    }

    /**
     * 语音转文字 - 百度
     * @param bytes 字节数组
     */
    fun voiceToText(bytes: ByteArray): String {
        // 初始化一个AipSpeech
        val client = AipSpeech("28912025", "bfqGYcs3aS11aaRGk024cs2o",
            "o50Gib8EsFVDHgaPhSc3hwjBFak8mlpN")

        // 设置网络连接参数
        client.setConnectionTimeoutInMillis(2000)
        client.setSocketTimeoutInMillis(60000)

        //
        val options = hashMapOf<String, Any>("dev_pid " to 1537)

        //
        val response = JSON.parseObject(client.asr(bytes, "pcm", 16000, options).toString(),
            BaiduResponse::class.java)
        logger.info("百度语音转文字：{}", response)
        if (response.err_no != 0) {
            return ""
        }
        return response.result[0]
    }
}